package com.business.system.official.toursism.websitenews.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.business.system.official.toursism.websitenews.entity.WebsiteNews;
import com.business.system.official.toursism.websitenews.from.WebsiteNewsSaveFrom;
import com.business.system.official.toursism.websitenews.from.WebsiteNewsUpdateFrom;
import com.business.system.official.toursism.websitenews.manager.WebsiteNewsManager;
import com.business.system.official.toursism.websitenews.service.WebsiteNewsService;
import com.business.system.official.toursism.websitenews.vo.data.WebsiteNewsDetailData;
import com.seed.core.exception.ManagerException;
import com.seed.core.exception.ServiceException;
import com.seed.core.pojo.controller.SeedSystemController;
import com.seed.core.pojo.dto.SeedMessageDTO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.aspect.annotation.AutoLog;
import org.jeecg.common.system.query.QueryGenerator;
import org.jeecgframework.poi.excel.def.NormalExcelConstants;
import org.jeecgframework.poi.excel.entity.ExportParams;
import org.jeecgframework.poi.excel.view.JeecgEntityExcelView;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.List;

/**
 * @description 乌镇新闻
 * @author CodeGenerator
 * @date   2025-01-25
 * @version V1.0
 */
@Slf4j
@RestController("websiteNewsController")
@Api(tags="乌镇新闻")
@RequestMapping("/official/toursism/websitenews")
public class WebsiteNewsController extends SeedSystemController<WebsiteNewsService, WebsiteNewsManager, WebsiteNews> {

    /**
     * 分页列表查询
     *
     * @param websiteNews
     * @param pageNo
     * @param pageSize
     * @param req
     * @return
     */
    @AutoLog(value = "乌镇新闻-分页列表查询")
    @ApiOperation(value = "乌镇新闻-分页列表查询", notes = "乌镇新闻-分页列表查询")
    @GetMapping(value = "/list")
    public Result<IPage<WebsiteNewsDetailData>> queryPageList(WebsiteNews websiteNews,
                                                         @RequestParam(name = "pageNo", defaultValue = "1") Integer pageNo,
                                                         @RequestParam(name = "pageSize", defaultValue = "10") Integer pageSize,
                                                         HttpServletRequest req) {
        Result<IPage<WebsiteNewsDetailData>> result = new Result<>();
        QueryWrapper<WebsiteNews> queryWrapper = QueryGenerator.initQueryWrapper(websiteNews, req.getParameterMap());
        Page<WebsiteNews> page = new Page<>(pageNo, pageSize);
        IPage<WebsiteNewsDetailData> pageList;
        try {
            pageList = baseService.pageVO(page, queryWrapper, websiteNews);
        } catch (ServiceException e) {
            result.setCode(500);
            result.setSuccess(false);
            result.setMessage("查询失败");
            return result;
        }
        return Result.OK(pageList);
    }


    /**
     * 列表查询
     *
     * @param websiteNews
     * @param req
     * @return
     */
    @AutoLog(value = "乌镇新闻-列表查询")
    @ApiOperation(value = "乌镇新闻-列表查询", notes = "乌镇新闻-列表查询")
    @GetMapping(value = "/all")
    public Result<List<WebsiteNewsDetailData>> queryList(WebsiteNews websiteNews,
                                                    HttpServletRequest req) {
        Result<List<WebsiteNewsDetailData>> result = new Result<>();
        QueryWrapper<WebsiteNews> queryWrapper = QueryGenerator.initQueryWrapper(websiteNews, req.getParameterMap());
        List<WebsiteNewsDetailData> list;
        try {
            list = baseManager.listConvertDetailData(queryWrapper);
        } catch (ManagerException e) {
            result.setCode(500);
            result.setSuccess(false);
            result.setMessage("查询失败");
            return result;
        }
        return result.success("成功", list);
    }

    /**
     * 添加
     *
     * @param websiteNews
     * @return
     */
    @AutoLog(value = "乌镇新闻-添加")
    @ApiOperation(value = "乌镇新闻-添加", notes = "乌镇新闻-添加")
    @PostMapping(value = "/add")
    public Result<WebsiteNewsDetailData> add(@RequestBody WebsiteNewsSaveFrom websiteNews) {
        Result<WebsiteNewsDetailData> result = new Result<>();
        if (StringUtils.isEmpty(websiteNews)) {
            return result.error500("添加数据不能为空！");
        }
        SeedMessageDTO baseSeedDTO;
        try {
            baseSeedDTO = baseService.save(websiteNews);
        } catch (ServiceException e) {
            result.setCode(500);
            result.setSuccess(false);
            result.setMessage("保存失败");
            return result;
        }
        if (!baseSeedDTO.getSuccess()) {
            return result.error500(baseSeedDTO.getMessage());
        }
        return result.success(baseSeedDTO.getMessage());
    }

    /**
     * 编辑
     *
     * @param websiteNews
     * @return
     */
    @AutoLog(value = "乌镇新闻-编辑")
    @ApiOperation(value = "乌镇新闻-编辑", notes = "乌镇新闻-编辑")
    @PutMapping(value = "/edit")
    public Result<WebsiteNewsDetailData> edit(@RequestBody WebsiteNewsUpdateFrom websiteNews) {
        Result<WebsiteNewsDetailData> result = new Result<>();
        if (StringUtils.isEmpty(websiteNews)) {
            return result.error500("编辑数据不能为空!");
        }
        if (StringUtils.isEmpty(websiteNews.getId())) {
            return result.error500("编辑数据不能为空!");
        }
        SeedMessageDTO baseSeedDTO;
        try {
            baseSeedDTO = baseService.updateById(websiteNews);
        } catch (ServiceException e) {
            result.setCode(500);
            result.setSuccess(false);
            result.setMessage("更新失败");
            return result;
        }
        if (!baseSeedDTO.getSuccess()) {
            return result.error500(baseSeedDTO.getMessage());
        }
        return result.success(baseSeedDTO.getMessage());
    }

    /**
     * 通过id查询
     *
     * @param id
     * @return
     */
    @AutoLog(value = "乌镇新闻-通过id查询")
    @ApiOperation(value = "乌镇新闻-通过id查询", notes = "乌镇新闻-通过id查询")
    @GetMapping(value = "/queryById")
    public Result<WebsiteNewsDetailData> queryById(@RequestParam(name = "id", required = true) String id) {
        Result<WebsiteNewsDetailData> result = new Result<>();
        WebsiteNewsDetailData websiteNews;
        try {
            websiteNews = baseManager.convertDetailData(id);
        } catch (ManagerException e) {
            result.setCode(500);
            result.setSuccess(false);
            result.setMessage("查询异常");
            return result;
        }
        if (websiteNews == null) {
            result.setCode(500);
            result.setSuccess(false);
            result.setMessage("未找到对应数据");
            return result;
        }
        return Result.OK(websiteNews);
    }

    /**
     * 通过id删除
     *
     * @param id
     * @return
     */
    @AutoLog(value = "乌镇新闻-通过id删除")
    @ApiOperation(value = "乌镇新闻-通过id删除", notes = "乌镇新闻-通过id删除")
    @DeleteMapping(value = "/delete")
    public Result<WebsiteNews> delete(@RequestParam(name = "id", required = true) String id) {
        Result<WebsiteNews> result = new Result<>();
        if (StringUtils.isEmpty(id)) {
            return result.error500("请求id为空!");
        }
        SeedMessageDTO baseSeedDTO;
        try {
            baseSeedDTO = baseService.removeById(id);
        } catch (ServiceException e) {
            result.setCode(500);
            result.setSuccess(false);
            result.setMessage("删除失败");
            return result;
        }
        if (!baseSeedDTO.getSuccess()) {
            return result.error500(baseSeedDTO.getMessage());
        }
        return result.success(baseSeedDTO.getMessage());
    }

    /**
     * 批量删除
     *
     * @param ids
     * @return
     */
    @AutoLog(value = "乌镇新闻-批量删除")
    @ApiOperation(value = "乌镇新闻-批量删除", notes = "乌镇新闻-批量删除")
    @DeleteMapping(value = "/deleteBatch")
    public Result<WebsiteNews> deleteBatch(@RequestParam(name = "ids", required = true) String ids) {
        Result<WebsiteNews> result = new Result<>();
        if (StringUtils.isEmpty(ids)) {
            return result.error500("请求id为空!");
        }
        List<String> idList = Arrays.asList(ids.split(","));
        SeedMessageDTO baseSeedDTO;
        try {
            baseSeedDTO = baseService.removeByIds(idList);
        } catch (ServiceException e) {
            result.setCode(500);
            result.setSuccess(false);
            result.setMessage("删除失败");
            return result;
        }
        if (!baseSeedDTO.getSuccess()) {
            return result.error500(baseSeedDTO.getMessage());
        }
        return result.success(baseSeedDTO.getMessage());
    }

    /**
     * 导出excel模板
     */
    @RequestMapping(value = "/exportXls/template")
    public ModelAndView exportXlsTemplate() {
        String title = "乌镇新闻模板信息";
        ModelAndView mv = new ModelAndView(new JeecgEntityExcelView());
        mv.addObject(NormalExcelConstants.FILE_NAME, title);
        mv.addObject(NormalExcelConstants.CLASS, this.realEntityClass);
        mv.addObject(NormalExcelConstants.PARAMS, new ExportParams(title + "报表", "导出时间:" + new SimpleDateFormat("yyyy 年 MM 月 dd 日 E HH 点 mm 分 ss 秒").format(new Date()), title));
        mv.addObject(NormalExcelConstants.DATA_LIST, new ArrayList<>());
        return mv;
    }

    /**
     * 导出excel
     *
     * @param request
     * @param websiteNews
     */
    @RequestMapping(value = "/exportXls")
    public ModelAndView exportXls(HttpServletRequest request, WebsiteNews websiteNews) {
        return super.exportXls(request, websiteNews, "乌镇新闻");
    }

    /**
     * 通过excel导入数据
     *
     * @param request
     * @return
     */
    @Override
    @RequestMapping(value = "/importExcel", method = RequestMethod.POST)
    public Result<?> importExcel(HttpServletRequest request) {
        return super.importExcel(request);
    }

}
